home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr11 / pdox693.zip / TI662.ASC < prev    next >
Text File  |  1993-01-05  |  8KB  |  265 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8. PRODUCT  :  Paradox                                      NUMBER  :  662
  9. VERSION  :  2.0 & up
  10.      OS  :  DOS
  11.    DATE  :  January 5, 1993                                PAGE  :  1/4
  12.  
  13.   TITLE  :  Using PAL to Generate Unique Values (Auto-increment)
  14.  
  15.  
  16.  
  17.  
  18. ┌───────────────────────────────────────────────────────────────┐
  19. │                                                               │
  20. │ This Technical Information Sheet is intended only for users   │
  21. │ who are familiar with PAL.  It contains PAL code to demon-    │
  22. │ strate a model solution to a specific problem and is not      │
  23. │ intended to represent a complete programming solution.        │
  24. │ Programmers who use this code must take responsibility for    │
  25. │ debugging and developing their application.  Assistance       │
  26. │ in debugging and developing applications is considered        │
  27. │ consulting and is beyond the scope of technical support.      │
  28. │                                                               │
  29. └───────────────────────────────────────────────────────────────┘
  30.  
  31. In many cases, you will want to generate unique values for each new
  32. record in a table.  For example, each new customer might need a new ID
  33. number.  Here are three different methods for creating an auto-
  34. increment capability.
  35.  
  36. The code samples here are generic examples only, and should be modified
  37. to fit your specific application.  Note: the following methods work
  38. only in Edit and Coedit modes.
  39.  
  40. For a single user in interactive mode, the following method works.
  41.  
  42.    SETKEY -23  [] = IMAGECMAX() + 1
  43.  
  44. Whenever the user presses <ALT-I>, the current field becomes equal to
  45. one more than the current maximum in the current column.  To use this
  46. command, insert the above command into the script INIT using the menu
  47. choices: Scripts | Editor | New (for versions earlier than 4.0,
  48. substitute Write for the menu choice New).  At the prompt, type in
  49. "Init" and press <ENTER>.  Type in the above command exactly as it
  50. appears.  The script INIT is played every time you start Paradox, thus
  51. activating <ALT-I>.  The keycode -23 corresponds to the key <ALT-I>.
  52. <ALT-I> is just one possible code that you can assign to the PAL
  53. script.  See Appendix G of the PAL Reference Guide for a list of
  54. Keycodes Recognized by Paradox (for versions earlier than 4.0, refer to
  55. Appendix B of the PAL User's Guide).  In order for the SETKEY macro to
  56. function properly, the cursor must be in the field to be incremented,
  57. which must be a numeric data type.
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74. PRODUCT  :  Paradox                                      NUMBER  :  662
  75. VERSION  :  2.0 & up
  76.      OS  :  DOS
  77.    DATE  :  January 5, 1993                                PAGE  :  2/4
  78.  
  79.   TITLE  :  Using PAL to Generate Unique Values (Auto-increment)
  80.  
  81.  
  82.  
  83.  
  84. The following is an example of a PAL program that increments a field:
  85.  
  86.    IF ISEMPTY("CUST") THEN
  87.        [CUST ID] = 1
  88.    ENDIF
  89.  
  90.    WHILE TRUE
  91.        IF ISBLANK([CUST ID]) THEN
  92.            [Cust ID] = CMAX("Cust","Cust ID") + 1
  93.        ENDIF
  94.  
  95.        WAIT RECORD UNTIL "F2", "INS", ...
  96.        KEYPRESS RETVAL
  97.        SWITCH
  98.            CASE RETVAL = "F2":
  99.                QUITLOOP
  100.            CASE ...
  101.                .
  102.                .
  103.                .
  104.        ENDSWITCH
  105.    ENDWHILE
  106.  
  107. (For more information on the PAL command WAIT RECORD see Technical
  108. Information Sheets 538 and 697)
  109.  
  110. Whenever the user moves to a record where the CUST ID is blank, the
  111. application enters in a new ID number.  The new ID number is derived
  112. using the CMAX function which returns the highest number.   The
  113. application then increments this number by one.
  114.  
  115. The previous two methods work fine in a single-user environment,
  116. however if there is a possibility the application might be used on a
  117. network, you should use the following method.  Note: the following
  118. method works only in Coedit mode.
  119.  
  120. First, create a table with a single Numeric field.  For example:
  121.  
  122.       ID ═╦═══ ID ═══╗
  123.           ║   10001  ║
  124.  
  125. This table contains the next number to be used when the application
  126. increments the field in the table you are entering data.
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140. PRODUCT  :  Paradox                                      NUMBER  :  662
  141. VERSION  :  2.0 & up
  142.      OS  :  DOS
  143.    DATE  :  January 5, 1993                                PAGE  :  3/4
  144.  
  145.   TITLE  :  Using PAL to Generate Unique Values (Auto-increment)
  146.  
  147.  
  148.  
  149.  
  150. Whenever the user moves to a record where the CUST ID is blank, a new
  151. record is created and filled in with a new ID number.  The main data
  152. entry routine calls a procedure that moves to the ID table, and
  153. attempts to lock the record.  If it fails the first time, it keeps
  154. attempting to lock the record until it succeeds.  During these
  155. attempts, no one else can access the number there.  This method
  156. guarantees that each user obtains a unique number.
  157.  
  158. When the application succeeds in locking the number, it increments it
  159. by one so the next time the application accesses a the table a new
  160. number is available.  Next, the application moves back to the CUST
  161. table to continue the editing.
  162.  
  163. The ID table must be on the workspace in order to access it.  If data
  164. entry occurs in table view or with a single table form, you must place
  165. the ID table on the workspace before editing your data table.
  166.  
  167. A multi-table form complicates the process of accessing the ID table.
  168. (In a multi-table form it is not possible to toggle to table view
  169. before posting the current record.)   The simplest solution is to place
  170. the ID table into your master form as an unlinked embedded form, with
  171. the foreground color the same color as the background to hide it from
  172. the users.  With the ID table embedded on the master table, the
  173. application can access the ID table while remaining in form view.
  174.  
  175. The following procedure is an example of how to access the ID table to
  176. obtain the next ID number.
  177.  
  178.    PROC GetCustID()
  179.    PRIVATE id
  180.        CurrTab = TABLE()
  181.        MOVETO "ID"
  182.        LOCKRECORD              ;Give only one user access
  183.        WHILE NOT RETVAL        ;Keep trying if first attempt
  184.            SLEEP 10            ;fails
  185.            LOCKRECORD
  186.        ENDWHILE
  187.        id = [ID]
  188.        [ID] = [ID] + 1         ;Increment ID
  189.        UNLOCKRECORD            ;Allow other users access
  190.        MOVETO CurrTab
  191.        RETURN id
  192.    ENDPROC
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. PRODUCT  :  Paradox                                      NUMBER  :  662
  207. VERSION  :  2.0 & up
  208.      OS  :  DOS
  209.    DATE  :  January 5, 1993                                PAGE  :  4/4
  210.  
  211.   TITLE  :  Using PAL to Generate Unique Values (Auto-increment)
  212.  
  213.  
  214.  
  215.  
  216. The following PAL program is an example of the main data entry routine.
  217.  
  218.    WHILE TRUE
  219.  
  220.        IF ISBLANK([Cust ID]) THEN
  221.            [Cust ID] = GetCustID()
  222.        ENDIF
  223.  
  224.        WAIT RECORD
  225.        UNTIL "F2", "INS" ...
  226.  
  227.        SWITCH
  228.            CASE RETVAL = "F2"  :
  229.                .
  230.                .
  231.                .
  232.        ENDSWITCH
  233.    ENDWHILE
  234.  
  235.  
  236.  
  237. Paradox 4.0 Application Workshop
  238.  
  239. Instead of writing the whole data entry routine yourself, you could
  240. attach GetCustID() to the ARRIVEROW trigger in the Event Procs for an
  241. Edit session.  This works best if the ID table is an embedded unlinked
  242. table.  You will need to add additional code to make sure that it only
  243. updates the ID field:
  244.  
  245.    IF TABLE() = "Master" and ISBLANK([Cust ID]) THEN
  246.        [Cust ID] = GetCustID()
  247.    ENDIF
  248.  
  249. DISCLAIMER: You have the right to use this technical information
  250. subject to the terms of the No-Nonsense License Statement that you
  251. received with the Borland product to which this information pertains.
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.